package com.cat.twoPointerTechnique;

import java.util.Arrays;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/valid-triangle-number/
 * @create 2025/8/1 16:21
 * @since JDK17
 */

public class Solution04 {
    public int triangleNumber(int[] nums) {
        Arrays.sort(nums);  // 排升序
        int ans = 0, n = nums.length;
        for (int i = 0; i < n - 2; i++) {
            for (int j = i + 1, k = j + 1; j < k; j++) {
                int sum = nums[i] + nums[j];    // 两边之和大于第三边
                while (nums[k] < sum) k++;
                ans += k - j - 1;
            }
        }
        return ans;
    }
}
